package com.yingxin.yx.framework.commons.utlis;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 时间验证
 */
public class TimeUtils {
    /**
     * 验证是否当天
     * @param createTime
     * @return
     */
    public static boolean isToday(Date createTime){
        Calendar c1 = Calendar.getInstance();
        c1.setTime(createTime);
        int year1 = c1.get(Calendar.YEAR);
        int month2 = c1.get(Calendar.MONTH)+1;
        int day1 = c1.get(Calendar.DAY_OF_MONTH);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(new Date());
        int year2 = c2.get(Calendar.YEAR);
        int month3 = c2.get(Calendar.MONTH)+1;
        int day2 = c2.get(Calendar.DAY_OF_MONTH);
        if(year1 == year2 && month2 == month3 && day1 == day2){
            return true;
        }
        return false;
    }

    /**
     * 判断一个时间在不在一个时间段
     * @param
     */
    public static void isTodayTime(Date createTime){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format.format(createTime);

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localTime = LocalDateTime.parse(time, dtf);
        //时间段
        LocalDateTime startTime = LocalDate.now().atTime(0, 0, 0);
        LocalDateTime endTime = LocalDate.now().atTime(23, 59, 59);
        //如果小于今天的开始日期
        if (localTime.isBefore(startTime)) {
            System.out.println("以前");
        }
        //如果大于今天的开始日期，小于今天的结束日期
        if (localTime.isAfter(startTime) && localTime.isBefore(endTime)) {
            System.out.println("当天");
        }
        //如果大于今天的结束日期
        if (localTime.isAfter(endTime)) {
            System.out.println("以后");
        }
    }

    /**
     * 获取当前时间前一秒时间
     * 解决流程转办顺序
     * @param date
     * @return
     */
    public static Date frontDate(Date date){
        Calendar c = new GregorianCalendar();
        c.setTime(date);//设置参数时间
        c.add(Calendar.SECOND,-1);//把日期往后增加SECOND 秒.整数往后推,负数往前移动
        return c.getTime();
    }

}
